﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using E_Commerce.Domain.Interfaces;
using Dapper;
using Microsoft.Extensions.Configuration;
using E_Commerce.Domain.Entities;

namespace E_Commerce.Infrastructure.Data
{
    public class ProductCategoryRepository : DapperRepository<ProductCategory>, IProductCategoryRepository
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private IConfiguration _configuration;
        public ProductCategoryRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            this.ConnStr = _configuration.GetConnectionString("ConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public async Task<int> AddCateAsync(ProductCategory p)
        {
            try
            {
                return (int)await Conn.InsertAsync(p);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleCateAsync(int id)
        {
            try
            {
                string sql = $"delete from ProductCategory where CateId={id}";
                return await Conn.ExecuteAsync(sql);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
           
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProductCategory> FillCateAsync(int id)
        {
            return await Conn.GetAsync<ProductCategory>(id);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<ProductCategory> GetProductCategories()
        {
            string sql = $"select * from ProductCategory";
            return Conn.Query<ProductCategory>(sql).ToList();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public async Task<int> UptCateAsync(ProductCategory p)
        {
            try
            {
                return await Conn.UpdateAsync(p);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
    }
}
